package forms;

import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import models.CategoryModel;

public class CategoryForm {
	public TreeMap checkValid(HttpServletRequest request, CategoryModel category) {
		TreeMap form = new TreeMap();
		TreeMap check = new TreeMap();
		String name = (String) request.getParameter("name");
		String desc = (String) request.getParameter("desc");
		String parentId = (String) request.getParameter("parentId");
		String keywords = (String) request.getParameter("keywords");
		String metaDesc = (String) request.getParameter("metaDesc");
		String status = (String) request.getParameter("status");
		
		if (name == null || name.equals("")) {
			check.put("name", "Name is not empty!");
			category.setName(name);
		} else {
			category.setName(name);
		}
		
		if (desc == null || desc.equals("")) {
			check.put("desc", "Description is not empty");
			category.setDesc(desc);
		} else {
			category.setDesc(desc);
		}
		
		if (parentId != null) {
			try {
				category.setParentId(new Long(parentId));
			} catch (Exception e) {
				check.put("parentId", "Parent must is number!");
			}
		}
		
		if (keywords == null || keywords.equals("")) {
			check.put("keywords", "META Keywords is not empty");
			category.setKeywords(keywords);
		} else {
			category.setKeywords(keywords);
		}
		
		if (metaDesc == null || metaDesc.equals("")) {
			check.put("metaDesc", "META Description is not empty");
			category.setMetaDesc(metaDesc);
		} else {
			category.setMetaDesc(metaDesc);
		}
		
		if (status != null && !status.equalsIgnoreCase("")) {
			category.setStatus(Integer.parseInt(status));
		}
		
		form.put("checkValid", check);
		form.put("category", category);
		
		return form;
	}
}
